// Browser-compatible Google Sheets Service
// This file will be loaded in the browser environment

// 模拟 Google Sheets 服务对象
window.GoogleSheetsService = {
    // 获取 Sheet 名称
    async getSheetNames(sheetId) {
        try {
            // 在浏览器环境中，我们需要通过 IPC 与主进程通信
            if (window.electronAPI && window.electronAPI.getSheetNames) {
                return await window.electronAPI.getSheetNames(sheetId);
            } else {
                // 如果没有 electronAPI，返回模拟数据
                console.warn('electronAPI not available, using mock data');
                return ['Sheet1', 'Sheet2'];
            }
        } catch (error) {
            console.error('Error getting sheet names:', error);
            throw error;
        }
    },

    // 读取行数据
    async readRowData(sheetId, range) {
        try {
            if (window.electronAPI && window.electronAPI.readRowData) {
                return await window.electronAPI.readRowData(sheetId, range);
            } else {
                console.warn('electronAPI not available, using mock data');
                return []; // 返回空数组作为模拟数据
            }
        } catch (error) {
            console.error('Error reading row data:', error);
            throw error;
        }
    },

    // 更新部分列数据
    async updatePartialColumns(sheetId, sheetName, plmCode, updates, range) {
        try {
            if (window.electronAPI && window.electronAPI.updatePartialColumns) {
                return await window.electronAPI.updatePartialColumns(sheetId, sheetName, plmCode, updates, range);
            } else {
                console.warn('electronAPI not available, simulating update');
                return { success: true };
            }
        } catch (error) {
            console.error('Error updating partial columns:', error);
            throw error;
        }
    },

    // 获取用户设置
    async getUserSettings() {
        try {
            if (window.electronAPI && window.electronAPI.getUserSettings) {
                return await window.electronAPI.getUserSettings();
            } else {
                console.warn('electronAPI not available, using default settings');
                return {
                    email: 'user@example.com',
                    site: 'Default Site'
                };
            }
        } catch (error) {
            console.error('Error getting user settings:', error);
            throw error;
        }
    },

    // 发送监控数据
    async sendMonitoringData(data) {
        try {
            if (window.electronAPI && window.electronAPI.sendMonitoringData) {
                return await window.electronAPI.sendMonitoringData(data);
            } else {
                console.warn('electronAPI not available, skipping monitoring data');
                return { success: true };
            }
        } catch (error) {
            console.error('Error sending monitoring data:', error);
            throw error;
        }
    }
};

// 初始化服务
document.addEventListener('DOMContentLoaded', function() {
    // 将服务对象赋值给全局变量
    window.googleSheetsService = window.GoogleSheetsService;
    console.log('Google Sheets Service initialized for browser environment');
});